package com.yangguang.huahui.ui.activitys;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yangguang.huahui.Config;
import com.yangguang.huahui.R;
import com.yangguang.huahui.app.App;
import com.yangguang.huahui.utils.CommonUtil;
import com.yangguang.huahui.utils.IDCard;

import java.text.ParseException;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

public class InputActivity extends BaseActivity implements TextView.OnEditorActionListener {
    @BindView(R.id.et_name)
    EditText et_name;
    @BindView(R.id.tv_input_title)
    TextView tv_input_title;
    @BindView(R.id.mFL_input)
    FrameLayout mFL_input;
    @BindView(R.id.mFL_phone)
    FrameLayout mFL_phone;
    @BindView(R.id.et_phone)
    EditText et_phone;//手机号
    @BindView(R.id.mLL_IDCard)
    LinearLayout mLL_IDCard;//身份证
    @BindView(R.id.et_IDCard)
    EditText et_IDCard;


    @BindView(R.id.tv_name_length)
    TextView tv_name_length;

    private int type;
    private int search_type;
    private int length;
    private int edit_type;

    @OnClick({R.id.iv_close, R.id.iv_ok, R.id.iv_clear, R.id.tv_search})
    void onClick(View v){
        switch (v.getId()){
            case R.id.iv_close:
                finish();
                break;
            case R.id.iv_clear:
                et_phone.setText("");
                break;
            case R.id.iv_ok:
                submit();
                break;
            case R.id.tv_search:
                submit();
                break;
        }
    }
    @Override
    protected int initLayoutId() {
        return R.layout.activity_input;
    }

    @Override
    protected void initView() {
        App.getInstance().addActivity((Activity) mContext);
    }

    @Override
    protected void initData() {
        init();
    }

    private void init(){
        if (!CommonUtil.isNull(getIntent().getStringExtra("input_title"))) {
            tv_input_title.setText(getIntent().getStringExtra("input_title"));
        }
        if (!CommonUtil.isNull(getIntent().getStringExtra("input_content"))) {
            et_name.setText(getIntent().getStringExtra("input_content"));
            et_phone.setText(getIntent().getStringExtra("input_content"));
            et_IDCard.setText(getIntent().getStringExtra("input_content"));
        } else {
            et_name.setHint("请输入" + getIntent().getStringExtra("input_title"));
            et_phone.setHint("请输入手机号/座机号");
            et_IDCard.setHint("请输入身份证号");
        }

        type = getIntent().getIntExtra("input_type", 0);
        search_type = getIntent().getIntExtra("search_type", 0);
        edit_type = getIntent().getIntExtra("edit_type", 0);
        length = getIntent().getIntExtra("input_length", -1);
        if (length > 0) {
            tv_name_length.setText(length + "");
        }
        if (edit_type != 0) {
            et_name.setInputType(edit_type);
        }
        if (edit_type == 101) {
            mFL_input.setVisibility(View.GONE);
            mFL_phone.setVisibility(View.VISIBLE);
            et_phone.setVisibility(View.VISIBLE);
        }
        if (type == 1000) {
            mFL_input.setVisibility(View.GONE);
            et_phone.setVisibility(View.GONE);
            mLL_IDCard.setVisibility(View.VISIBLE);
        }
        if (length > 0) {
            et_name.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)});
        }
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

                           public void run() {
                               InputMethodManager inputManager =
                                       (InputMethodManager) et_name.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                               inputManager.showSoftInput(et_name, 0);
                               InputMethodManager inputManager1 =
                                       (InputMethodManager) et_phone.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                               inputManager1.showSoftInput(et_phone, 0);
                               InputMethodManager inputManager2 =
                                       (InputMethodManager) et_IDCard.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                               inputManager2.showSoftInput(et_IDCard, 0);
                           }

                       },
                200);
        et_name.setSelection(et_name.getText().toString().length());
        et_phone.setSelection(et_phone.getText().toString().length());
        et_IDCard.setSelection(et_IDCard.getText().toString().length());

        et_name.setImeOptions(EditorInfo.IME_ACTION_DONE);
        et_IDCard.setImeOptions(EditorInfo.IME_ACTION_DONE);
        et_phone.setImeOptions(EditorInfo.IME_ACTION_DONE);
        et_name.setOnEditorActionListener(this);
        et_IDCard.setOnEditorActionListener(this);
        et_phone.setOnEditorActionListener(this);
        et_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (length > 0) {
                    tv_name_length.setText(String.valueOf(length - s.length()));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        if ((actionId == 0 || actionId == 3) && keyEvent != null) {
            submit();
            return true;
        }
        return false;
    }

    private void submit() {
        if (edit_type == 101) {
            if (!CommonUtil.isNull(et_phone.getText().toString())) {
                if (CommonUtil.isPhoneNumberValid(et_phone.getText().toString())) {
                    setResult(Config.INPUT_RESULT_CODE, new Intent().
                            putExtra("input", et_phone.getText().toString()).putExtra("input_type", type));
                    finish();
                } else {
                    CommonUtil.myToastA(mContext, "请输入正确的手机号/座机号");
                }
            } else {
                setResult(Config.INPUT_RESULT_CODE, new Intent().
                        putExtra("input", et_phone.getText().toString()).putExtra("input_type", type));
                finish();
            }
        } else if (edit_type == InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS) {
            if (CommonUtil.isEmail(et_name.getText().toString())) {
                setResult(Config.INPUT_RESULT_CODE, new Intent().
                        putExtra("input", et_name.getText().toString()).putExtra("input_type", type));
                finish();
            } else {
                CommonUtil.myToastA(mContext, getResources().getText(R.string.input_email_currect).toString());
            }
        } else {
            if (type == 1000) {
                String num = "";
                try {
                    num = IDCard.IDCardValidate(et_IDCard.getText().toString());
                    if ("".equals(num)) {
                        setResult(Config.INPUT_RESULT_CODE, new Intent().
                                putExtra("input", et_IDCard.getText().toString()).putExtra("input_type", type));
                        finish();
                    } else {
                        CommonUtil.myToastA(mContext, num);
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            } else {
                setResult(Config.INPUT_RESULT_CODE, new Intent().
                        putExtra("input", et_name.getText().toString()).putExtra("input_type", type));
                finish();
            }
        }
    }
}
